package cn.edu.djtu.fw.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.djtu.fw.model.Json;
import cn.edu.djtu.fw.model.Message;
import cn.edu.djtu.fw.service.MessageServiceI;
import cn.edu.djtu.fw.util.JsonUtil;

import com.opensymphony.xwork2.ActionSupport;

public class MessageAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private MessageServiceI messageService;
	private Message message;
	private int page;
	private int rows;
	private String ids;

	private String mid;
	private String gid;
	private String uid;
	private String realname;
	private String content;

	@Override
	public String execute() throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();
		m = messageService.getAllMessage(page, rows, message);
		JsonUtil.writeJson(m);
		return null;
	}

	@SuppressWarnings("unchecked")
	public String i_add() throws Exception {
		HttpSession session = request.getSession();
		Message m = new Message();
		if (gid != null && gid.length() > 0) {
			if (uid != null && uid.length() > 0) {
				if (realname != null && realname.length() > 0) {
					m.setGid(Integer.parseInt(gid));
					m.setUid(Integer.parseInt(uid));
					m.setRealname(realname);
					m.setCreatetime(new Date());
					m.setContent(content);
					messageService.insert(m);
					Message msg = new Message();
					msg.setGid(Integer.parseInt(gid));
					List<Message> messages = (List<Message>) messageService.getAllMessage(1, 10, msg).get("rows");
					session.setAttribute("msgdetail", messages);
					return "goodsShow2";
				}
			}
		}
		return "goodsShow2";
	}

	public String i_delete() throws Exception {
		HttpSession session = request.getSession();
		if (mid != null && mid.length() > 0) {
			Message c = messageService.getMessageById(Integer.parseInt(mid));
			if (c != null) {
				messageService.delete(c.getMid());
			}
			@SuppressWarnings("unchecked")
			List<Message> messages = (List<Message>) session.getAttribute("msgdetail");
			for (Message message : messages) {
				if (message.getMid() == Integer.parseInt(mid)) {
					messages.remove(message);
					break;
				}
			}
			session.setAttribute("msgdetail", messages);
		}
		return "goodsShow2";
	}
	public String i_delete2() throws Exception {
		HttpSession session = request.getSession();
		if (mid != null && mid.length() > 0) {
			Message c = messageService.getMessageById(Integer.parseInt(mid));
			if (c != null) {
				messageService.delete(c.getMid());
			}
			@SuppressWarnings("unchecked")
			List<Message> messages = (List<Message>) session.getAttribute("user_message_list");
			for (Message message : messages) {
				if (message.getMid() == Integer.parseInt(mid)) {
					messages.remove(message);
					break;
				}
			}
			session.setAttribute("user_message_list", messages);
		}
		return "user-msg";
	}
	@SuppressWarnings("unchecked")
	public String show_user_message_list() throws Exception {
		HttpSession session = request.getSession();
		if (Integer.parseInt(uid) > 0) {
			Message message = new Message();
			message.setUid(Integer.parseInt(uid));
			List<Message> user_message_list = (List<Message>) messageService.getAllMessage(1, 10, message).get("rows");
			if (user_message_list != null) {
				session.setAttribute("user_message_list", user_message_list);
				return "showUserMessage";
			} else {
				session.setAttribute("user_message_list", null);
				return "showUserMessage";
			}
		}
		return "showUserMessage";
	}

	public void add() throws Exception {
		Json j = new Json();
		message.setCreatetime(new Date());
		if (messageService.insert(message)) {
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} else {
			j.setMsg("添加失败！");
		}
		JsonUtil.writeJson(j);
	}

	public void update() throws Exception {
		Json j = new Json();
		if (messageService.update(message)) {
			j.setSuccess(true);
			j.setMsg("修改成功！");
		} else {
			j.setMsg("修改失败！");
		}
		JsonUtil.writeJson(j);
	}

	public void delete() throws Exception {
		Json j = new Json();
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			Message c = messageService.getMessageById(Integer.parseInt(idsStr[i]));
			if (c != null) {
				messageService.delete(c.getMid());
			}
		}
		j.setSuccess(true);
		j.setMsg("删除成功！");
		JsonUtil.writeJson(j);
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public MessageServiceI getMessageService() {
		return messageService;
	}

	@Autowired
	public void setMessageService(MessageServiceI messageService) {
		this.messageService = messageService;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

}
